/**
 * ContactActivity.java
 * 
 * Create Version: 1.0
 * Author: lixh
 * Create Date: 2014-9-6
 * 
 * Copyright (c) 2014 CQCIS. All Right Reserved.
 */
package com.yinhai.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;

import com.androidquery.AQuery;
import com.yinhai.adapter.ContactGroupContactListAdpter;
import com.yinhai.cfg.AppConfig;
import com.yinhai.constant.Contact;
import com.yinhai.model.ContactGroup;
import com.yinhai.model.ContactsUser;
import com.yinhai.service.ContactUserServiceImpl;

/**
 * ContactActivity （通讯录）
 * 
 * @author lixh
 */
public class ContactListActivity extends Activity {
	private ListView contactListView;
	private ContactGroupContactListAdpter smsAdpter;
	private ContactUserServiceImpl contactUserService;
	private List<ContactsUser> list;
	private int Isdxqf = 0; // 1短信群发 0,写短信
	private AQuery aq;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_list_contact);
		aq = new AQuery(this);
		contactUserService = new ContactUserServiceImpl(this);
		if (Contact.LOCAL_CONTACT_GROUP_ID.endsWith(getIntent().getStringExtra(
				"contactGroupId"))) {
			list = contactUserService.findLocalContact();
		} else {
			list = contactUserService.findContactsUserByGroupId(getIntent()
					.getStringExtra("contactGroupId"));
			list = ClearSelf(list);
		}
		if(list == null){
            list = new ArrayList<ContactsUser>();
        }
		Isdxqf = getIntent().getIntExtra("Isdxqf", 0);
		contactListView = (ListView) this.findViewById(R.id.contact_list_item);
		smsAdpter = new ContactGroupContactListAdpter(this, list, true);
		contactListView.setAdapter(smsAdpter);
		contactListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				CheckBox checkBox = (CheckBox) view
						.findViewById(R.id.btn_check);
				checkBox.setChecked(!checkBox.isChecked());
				list.get(Integer.valueOf(checkBox.getTag().toString()))
						.setCheck(checkBox.isChecked());
				smsAdpter.notifyDataSetChanged();
			}
		});
		findViewById(R.id.btn_li).setVisibility(View.VISIBLE);

		CheckBox cb = (CheckBox) findViewById(R.id.btn_check1);
		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				for (int i = 0; i < list.size(); i++) {
					list.get(i).setCheck(isChecked);
				}
				smsAdpter.assignment(list);
				smsAdpter.notifyDataSetChanged();
			}
		});

		aq.id(R.id.btn_back).clicked(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

	}

	public void doSelect(View view) {
		List<ContactsUser> users = new ArrayList<ContactsUser>();
		for (ContactsUser user : list) {
			if (user.isCheck()) {
				users.add(user);
			}
		}
		if (Isdxqf == 0) {
			Intent intent = new Intent(this, SMSDetailActivity.class);
			intent.putExtra("data", (Serializable) users);
			setResult(RESULT_OK, intent);
			finish();

		} else {
			Intent intent = new Intent(this, MsgQFActivity.class);
			intent.putExtra("data", (Serializable) users);
			setResult(RESULT_OK, intent);
			finish();

		}

	}

	public List<ContactsUser> ClearSelf(List<ContactsUser> listUser) {
		SharedPreferences preferences = getSharedPreferences(
				AppConfig.LOGIN_CONFIG, Context.MODE_PRIVATE);
		String phone = preferences.getString("name", "");
		if (listUser.size() > 0) {
			for (int i = 0; i < listUser.size(); i++) {
				if (listUser.get(i).getTelPhone().indexOf(phone) > -1) {
					listUser.remove(i);
					break;
				}
			}
		}
		return listUser;
	}
}
